/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import entities.*;
import entities.Domain;
import java.util.*;
import metadata.SystemMetaData;

/**
 *
 * @author Dragan Sahpaski;
 */
public class ISDC {

    Domain domain;
    Atribute itName = new Atribute("It_Name");
    Atribute itDesc = new Atribute("It_Desc");
    Atribute iName = new Atribute("I_Name");
    Atribute iPrice = new Atribute("I_Price");
    Atribute iStock = new Atribute("I_Stock");
    Atribute iDesc = new Atribute("I_Desc");
    Atribute sName = new Atribute("S_Name");
    Atribute sPhone1 = new Atribute("S_Phone1");
    Atribute sPhone2 = new Atribute("S_Phone2");
    Atribute rName = new Atribute("R_Name");
    Atribute rDesc = new Atribute("R_Desc");
    Atribute coName = new Atribute("Co_Name");
    Atribute coCode = new Atribute("Co_Code");
    Atribute coCont = new Atribute("Co_Cont");
    Atribute dDate = new Atribute("D_Date");
    Atribute dQuart = new Atribute("D_Quart");
    Atribute dMonth = new Atribute("D_Month");
    Atribute dYear = new Atribute("D_Year");
    Atribute cName = new Atribute("C_Name");
    Atribute cityName = new Atribute("C_Name");
    Atribute sdName = new Atribute("SD_Name");
    private static final int BLOCK_SIZE_BYTES = 200;
    private static final int HYBRID_HASH_JOIN_BUFFER_SIZE_BYTES = 100000000;

    public ISDC() throws Exception {
        Table itemType = new Relation("ItemType", 100, new PrimaryKey(new Atribute("It_Id")),
                new ArrayList<Atribute>(Arrays.asList(itName, itDesc)));
        Table item = new Relation("Item", 10000, new PrimaryKey(new Atribute("I_Id")),
                new ArrayList<Atribute>(Arrays.asList(iName, iDesc, iPrice, iStock)));

        Table country = new Relation("Country", 100, new PrimaryKey(new Atribute("Co_Id")),
                new ArrayList<Atribute>(Arrays.asList(coName, coCode, coCont)));
        Table region = new Relation("Region", 5000, new PrimaryKey(new Atribute("R_Id")),
                new ArrayList<Atribute>(Arrays.asList(rName, rDesc)));
        Table supplier = new Relation("Supplier", 10000, new PrimaryKey(new Atribute("S_Id")),
                new ArrayList<Atribute>(Arrays.asList(sName, sPhone1, sPhone2)));

        Table year = new Relation("Year", 4, new PrimaryKey(new Atribute("Y_Id")),
                new ArrayList<Atribute>(Arrays.asList(dYear)));
        Table month = new Relation("Month", 48, new PrimaryKey(new Atribute("M_Id")),
                new ArrayList<Atribute>(Arrays.asList(dMonth)));
        Table quart = new Relation("Quart", 16, new PrimaryKey(new Atribute("Q_Id")),
                new ArrayList<Atribute>(Arrays.asList(dQuart)));
        Table date = new Relation("Date", 1460, new PrimaryKey(new Atribute("D_Id")),
                new ArrayList<Atribute>(Arrays.asList(dDate)));

        Table sd = new Relation("SalesDistrict", 25, new PrimaryKey(new Atribute("SD_Id")),
                new ArrayList<Atribute>(Arrays.asList(sdName)));
        Table city = new Relation("City", 400, new PrimaryKey(new Atribute("C_Id")),
                new ArrayList<Atribute>(Arrays.asList(cityName)));
        Table cust = new Relation("Customer", 10000, new PrimaryKey(new Atribute("Cust_Id")),
                new ArrayList<Atribute>(Arrays.asList(cName)));


        Relation.addRelationship(itemType, item);
        Relation.addRelationship(Relation.getNullRelation(), itemType);
        Relation.addRelationship(Relation.getNullRelation(), country);
        Relation.addRelationship(country, supplier);
        Relation.addRelationship(region, supplier);
        Relation.addRelationship(Relation.getNullRelation(), year);
        Relation.addRelationship(year, month);
        Relation.addRelationship(month, date);
        Relation.addRelationship(quart, date);
        //Relation.addRelationship(sd, cust);
        //Relation.addRelationship(city, cust);
        Relation.addRelationship(Relation.getNullRelation(), cust);

        ViewGene root = new ViewGene("ISD", Arrays.asList(item, supplier, date, cust));
        ViewGraph vg = new ViewGraph(root);
        vg.buildViewGraph();
        // System.out.println(vg);
        domain = new Domain(vg, new SystemMetaData(BLOCK_SIZE_BYTES, HYBRID_HASH_JOIN_BUFFER_SIZE_BYTES));
        //System.out.println(Printing.printViews(domain));
        //System.out.println(Printing.printRelations(domain));
    }

    public Domain getDomain() {
        return domain;
    }

    public void setDomain(Domain domain) {
        this.domain = domain;
    }

    public static void main(String[] args) throws Exception {
        new ISDC();
    }
}
